<?php
declare (strict_types = 1);

namespace app\controller\api\v1\expert\mine;

use app\service\logic\common\finance\FinanceExpertBalanceService;
use app\service\logic\common\user\ExpertService;
use app\service\logic\common\user\ExpertStatisticsService;
use app\service\logic\common\user\ExpertWechatOpenService;
use app\service\logic\common\user\UserWechatOpenService;
use app\validate\common\finance\BalanceValidate;
use app\validate\common\user\UserValidate;
use app\validate\common\user\WechatOpenValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Request;
use think\App;
use think\response\Json;
use app\controller\api\ApiController;

class MineController extends ApiController
{
    protected $service;

    public function __construct(App $app, ExpertService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 我的信息
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function info()
    {
        $user = $this->request->middleware('user');
        $result = $this->service->read(['id' => $user['id']], []);
        return amis($result);
    }

    /**
     * 各总数
     *
     * @return Json
     */
    public function number()
    {
        $user = $this->request->middleware('user');
        $result = $this->service->getNumberByExpertId($user['id']);
        return amis($result);
    }

    /**
     * @return Json
     */
    public function amount()
    {
        $user = $this->request->middleware('user');
        $result = invoke(ExpertStatisticsService::class)->getAmountByExpertId($user['id']);
        return amis($result);
    }

    /**
     * 完善信息
     *
     * @param Request $request
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function complete(Request $request)
    {
        $params = $request->only($this->service->getUpdateColumn());
        $user = $this->request->middleware('user');
        $params['id'] = $user['id'];
        $this->validate($params, UserValidate::class . '.complete');

        $result = $this->service->item($params);
        return amis($result);
    }

    /**
     * 是否绑定微信
     *
     * @return Json
     */
    public function isBind()
    {
        $user = $this->request->middleware('user');
        $result = invoke(ExpertWechatOpenService::class)->isBindByExpertId($user['id']);
        return amis($result);
    }

    /**
     * 绑定微信
     *
     * @return Json
     */
    public function bind()
    {
        $params = $this->request->param();
        $this->validate($params, WechatOpenValidate::class . '.code');
        $user = $this->request->middleware('user');
        invoke(ExpertWechatOpenService::class)->bindByExpertIdCode($user['id'], $params);
        return amis();
    }

    /**
     * 提现
     *
     * @return Json
     */
    public function cash()
    {
        $params = $this->request->param();
        $this->validate($params, BalanceValidate::class . '.cash');
        $user = $this->request->middleware('user');
        $params['expert_id'] = $user['id'];

        $result = invoke(FinanceExpertBalanceService::class)->cashOperate($params);
        return amis($result);
    }

}
